//
//  ZJQuestionWebViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2018/1/16.
//  Copyright © 2018年 刘应平. All rights reserved.
//

#import "ZJQuestionWebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface ZJQuestionWebViewController ()<UIWebViewDelegate>
{
    NSDictionary *userInfo;
}
/** webView */
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ZJQuestionWebViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = self.titleString;
    [self.view addSubview:self.webView];
    userInfo = @{@"ACCOUNT_ID":[NSString isNilStringReturnString: ZJUSERINFORMODEL.ORG_ACCOUNT.ID],
                               @"MOBILE":[NSString isNilStringReturnString: ZJUSERINFORMODEL.ORG_ACCOUNT.MOBILE],
                               @"PARTYMEMBER_ID":[NSString isNilStringReturnString: ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID],
                               @"ZZ_DEPT_ID":[NSString isNilStringReturnString: ZJUSERINFORMODEL.DEPT_MODEL_JOB.ID],
                               @"ZC_DEPT_ID":[NSString isNilStringReturnString: ZJUSERINFORMODEL.DEPT_MODEL_BOOK.ID],
                               @"ZZ_DEPT_NAME":[NSString isNilStringReturnString: ZJUSERINFORMODEL.DEPT_MODEL_JOB.DEPT_NAME],
                               @"ZC_DEPT_NAME":[NSString isNilStringReturnString: ZJUSERINFORMODEL.DEPT_MODEL_BOOK.DEPT_NAME],
                               @"USER_NAME":[NSString isNilStringReturnString: ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME],
                               @"PHOTO_URL":[NSString isNilStringReturnString: ZJUSERINFORMODEL.ORG_ACCOUNT.PHOTO_URL],
                               };
    
    NSString * urlString = [NSString stringWithFormat:@"%@WeChat/question/questionnaireChoose.html?PAPER_ID=%@",BASEIMAGEURL,[NSString isNilStringReturnString:self.ID]];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}



- (void)action_leftButton:(UIButton *)sender{
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    NSString *textJS = @"window.webBack()";
    [context evaluateScript:textJS];
    
}
#pragma mark - UIWebView 协议方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    __weak typeof(self) weakSelf = self;

     JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    ///监听页面返回控制器
    context[@"webGoBack"] = ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    [YJProgressHUD hide];
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    __weak typeof(self) weakSelf = self;
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    [self.webView stringByEvaluatingJavaScriptFromString:@"window.getUserInfo()"];
    
    context[@"getUserInfo"] = ^{
        return [weakSelf jsonStringFromObject:userInfo];
    };
    ///监听页面返回控制器
    context[@"webGoBack"] = ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };

    
}
#pragma mark - Setter


#pragma mark - GettersASKTIME

- (UIWebView *)webView{
    if (_webView == nil){
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight )];
        _webView.delegate = self;
        _webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber | UIDataDetectorTypeLink | UIDataDetectorTypeShipmentTrackingNumber | UIDataDetectorTypeFlightNumber | UIDataDetectorTypeLookupSuggestion;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView setOpaque:YES];
    }
    return _webView;
}

@end
